package com.example.asus.testskip;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Toast;

public class CallActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    public void onClick(View view) {

    }

    public class MyPhoneCallListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            switch (state) {
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Toast.makeText(CallActivity.this, "正在通话中....", Toast.LENGTH_SHORT).show();
                    break;


                case TelephonyManager.CALL_STATE_RINGING:
                    Toast.makeText(CallActivity.this, incomingNumber, Toast.LENGTH_SHORT).show();
                    break;
            }


            super.onCallStateChanged(state, incomingNumber);
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        MyPhoneCallListener myPhoneCallListener = new MyPhoneCallListener();
        tm.listen(myPhoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(getApplicationContext())) {
                //启动Activity让用户授权
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, 100);
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {
                    Toast.makeText(this, "ACTION_MANAGE_OVERLAY_PERMISSION权限kaiqi", Toast.LENGTH_SHORT).show();
                    ViewUtils.callView(CallActivity.this, "11111111111");
                } else {
                    Toast.makeText(this, "ACTION_MANAGE_OVERLAY_PERMISSION权限已被拒绝", Toast.LENGTH_SHORT).show();
                    ;
                }
            }

        }
    }
}
